import { onMounted, reactive, toRefs } from "vue"
import { getDetail, add2Car } from "@/apis/goods"
import { useRoute, useRouter } from "vue-router"
import { Toast } from "vant"
interface addGoodType {
    [prop: string]: any
}
export default function baseHook() {
    const route: any = useRoute()
    const router: any = useRouter()
    const state: addGoodType = reactive({
        productInfo: {}
    })
    onMounted(async () => {
        let { data } = await getDetail(route.params.id)
        state.productInfo = data
    })

    // 立即购买btn
    const buyNow = async () => {
        const { resultCode } = await addCar()
        if (resultCode === 200) go2car()
    }

    // 跳转购物车
    const go2car = () => {
        router.push('/car')
    }

    // 加入购物车btn
    const addCar = (): any => {
        return new Promise(async (resolve, reject) => {
            const { resultCode } = await add2Car({ goodsCount: 1, goodsId: state.productInfo.goodsId })
            if (resultCode === 200){
                Toast.success('添加成功');
                resolve({ resultCode })
            }
        })
    }

    return {
        state, buyNow, addCar,go2car
    }
}
